<%@page import="qldc.pm.tienan.service.persistence.LichSuXoaAnTichFinderUtil"%>
<%@page import="qldc.pm.tienan.service.LichSuXoaAnTichLocalServiceUtil"%>
<%@page import="qldc.pm.tienan.model.TienAn"%>
<%@page import="qldc.pm.tienan.model.DoiTuongTienAn"%>
<%@page import="qldc.pm.tienan.service.TienAnLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.enumclass.EnumGioiTinh"%>
<%@page import="qldc.pm.tienan.constants.TrangThaiTienAnEnum"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="qldc.pm.qtht.dmdc.service.TinhLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.Tinh"%>
<%@page import="java.util.List"%>
<%@page import="qldc.pm.tienan.service.DoiTuongTienAnLocalServiceUtil"%>
<%@page import="qldc.pm.tienan.searchcontainer.DSTienAnSearchTerms"%>
<%@page import="qldc.pm.tienan.searchcontainer.DSTienAnDisplayTerms"%>
<%@page import="qldc.pm.tienan.searchcontainer.DSTienAnSearch"%>
<%@page import="java.util.Date"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="java.text.SimpleDateFormat"%>

<%@include file="../../init.jsp"%>

<%
	PortletURL portletURL = renderResponse.createRenderURL();
	portletURL.setParameter("mvcPath", "/html/view.jsp");
	portletURL.setParameter("chonChucNangItem",		TienAnConstants.DS_BKTT);
	portletURL.setParameter("loaiBKTK",	TienAnConstants.DS_XOA_AN_TICH);
	portletURL.setParameter("tabs1",TienAnConstants.DS_XOA_AN_TICH);

	PortletURL thoatUrl =  renderResponse.createRenderURL();
	thoatUrl.setParameter("mvcPath", "/html/view.jsp");
	thoatUrl.setParameter("chonChucNangItem",
	TienAnConstants.BAO_CAO_THONG_KE);
	String pathThemeImages = themeDisplay.getPathThemeImages();
	SimpleDateFormat dateFormat = new SimpleDateFormat(TienAnConstants.DATE_PATTERN);
%>

<portlet:resourceURL var="loadQuanHuyen" id="loadQuanHuyen">
	<portlet:param name="tinhIdSelected" value="{0}" />
</portlet:resourceURL>

<portlet:resourceURL var="loadPhuongXa" id="loadPhuongXa">
	<portlet:param name="quanHuyenIdSelected" value="{0}" />
</portlet:resourceURL>


<script type="text/javascript">
var NhapNgay = (function() {
	function NhapNgay() {}
	/*
	 * 
	 */
	 NhapNgay.prototype.initCalendars = function() {
			$("#<portlet:namespace/>tuNgayXoaAnTich").datepicker({
				showOn: "both",
				buttonImage: "<%=pathThemeImages%>/common/calendar.png",
					buttonImageOnly : true,
					buttonText : "<liferay-ui:message key="tuNgayPhamToi" />",
					dateFormat : "dd/mm/yy",
					onClose: function( selectedDate ) {
						 $("#<portlet:namespace/>denNgayXoaAnTich").datepicker( "option", "minDate", selectedDate );
						 }
				});
			$("#<portlet:namespace/>denNgayXoaAnTich").datepicker({
				showOn: "both",
				buttonImage: "<%=pathThemeImages%>/common/calendar.png",
					buttonImageOnly : true,
					buttonText : "<liferay-ui:message key="denNgayPhamToi" />",
					dateFormat : "dd/mm/yy",
					onClose: function( selectedDate ) {
						 $("#<portlet:namespace/>tuNgayXoaAnTich").datepicker( "option", "maxDate", selectedDate );
						 }
				});

		};

		return NhapNgay;
	})();

	$(function() {
		var dom = new NhapNgay();

		dom.initCalendars();
	});
</script>

<portlet:actionURL var="inDanhSachNguoiDaXoaAnTichURL">
	<portlet:param name="<%=TienAnConstants.ACTION_HANDLER_PARAM%>" value="<%=TienAnConstants.REPORT_ACTION%>" />
	<portlet:param name="<%=Constants.ACTION%>" value="inReportDSXoaAnTich" />
	<portlet:param name="redirect" value="<%=portletURL.toString()%>" />
</portlet:actionURL>

<div class="main-Content">

	<form action="<%=inDanhSachNguoiDaXoaAnTichURL.toString()%>" id="<portlet:namespace/>frm" method="POST" target="_blank">
		<%
			SimpleDateFormat sdf = new SimpleDateFormat(TienAnConstants.DATE_PATTERN);
			DSTienAnSearch DSTienAnSearch = new DSTienAnSearch(renderRequest,
					portletURL);
			DSTienAnDisplayTerms displayTerms = (DSTienAnDisplayTerms) DSTienAnSearch
					.getDisplayTerms();
			DSTienAnSearchTerms searchTerms = (DSTienAnSearchTerms) DSTienAnSearch
					.getSearchTerms();
			int sttDoiTuong = DSTienAnSearch.getDelta() * (DSTienAnSearch.getCur() - 1);
			long noiThuongTruTinhIdSelected = searchTerms.getTinhId();
			long noiThuongTruQuanHuyenIdSelected = searchTerms.getQuanHuyenId();
			long noiThuongTruPhuongXaIdSelected = searchTerms.getPhuongXaId();
			
			int totalRow = DoiTuongTienAnLocalServiceUtil.countByDS_XoaAnTich
					(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(),
							searchTerms.getTuNgayXoaAnTich(),
							searchTerms.getDenNgayXoaAnTich());
		%>
		<table class="gridnormal" style="padding-top: 10px; padding-left: 10px">
			<tr>
				<td class="aui-w20"><liferay-ui:message key="vung-dia-ly" /></td>

				<td class="aui-w80"><select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSTienAnDisplayTerms.TINH_ID%>" id="<portlet:namespace/>noiThuongTruTinhId"
					onchange="loadQuanHuyen('<portlet:namespace />noiThuongTruTinhId', '<portlet:namespace />noiThuongTruQuanHuyenId', '<portlet:namespace />noiThuongTruPhuongXaId', '<%=loadQuanHuyen.toString()%>','<%=noiThuongTruQuanHuyenIdSelected%>');">
						<option value="0"><liferay-ui:message key="chon-tinh" /></option>
						<%
							List<Tinh> tinhs = TinhLocalServiceUtil.findAll();

											for (Tinh tinh : tinhs) {
						%>
						<option value="<%=tinh.getTinhId()%>" <%=(noiThuongTruTinhIdSelected == tinh.getTinhId()|| tinh.getLaMacDinh()) ? "selected=selected": ""%>>
							<%=tinh.getTenTinh()%></option>
						<%
							}
						%>
				</select> <select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSTienAnDisplayTerms.QUAN_HUYEN_ID%>" id="<portlet:namespace/>noiThuongTruQuanHuyenId"
					onchange="loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString()%>', '<%=noiThuongTruPhuongXaIdSelected%>');">
						<option value="0"><liferay-ui:message key="chon-quan-huyen" /></option>

				</select> <select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSTienAnDisplayTerms.PHUONG_XA_ID%>" id="<portlet:namespace/>noiThuongTruPhuongXaId">
						<option value="0"><liferay-ui:message key="chon-phuong-xa" /></option>
				</select></td>

			</tr>
			<tr>
				<td class="txtlabel aui-w20"><liferay-ui:message key="ngay-xoa-an-tich-tu" /></td>
				<td class="aui-w80 ip-datepicker"><input type="text" id="<portlet:namespace/>tuNgayXoaAnTich" name="<portlet:namespace/><%=DSTienAnDisplayTerms.TU_NGAY_XOA_AN_TICH%>" value="<%=Validator.isNotNull(displayTerms.getTuNgayXoaAnTich())?sdf.format(displayTerms.getTuNgayXoaAnTich()) : ""%>" /> &nbsp;&nbsp;<liferay-ui:message key="den-ngay" />
					&nbsp;&nbsp;<input type="text" id="<portlet:namespace/>denNgayXoaAnTich" name="<portlet:namespace/><%=DSTienAnDisplayTerms.DEN_NGAY_XOA_AN_TICH%>" value="<%=Validator.isNotNull(displayTerms.getDenNgayXoaAnTich())?sdf.format(displayTerms.getDenNgayXoaAnTich()) : ""%>" /></td>

			</tr>
			<tr>
				<td></td>
				<td colspan="3">
					<div align="left">
						<input type=button onclick="timDanhSach();" id="<portlet:namespace/>Tim" class="btn btn-primary" name="<portlet:namespace/>Tim" value="<liferay-ui:message key="tim-kiem" />" /> <input type=button onclick="taoBaoCao();" <%=totalRow == 0?"disabled='disabled'":""%> id="<portlet:namespace/>BaoCao" class="btn btn-primary"
							name="<portlet:namespace/>BaoCao" value="<liferay-ui:message key="xuat-bao-cao" />" /> <input type=button class="btn" name="<portlet:namespace/>thoat" value="<liferay-ui:message key="tro-ve" />" onclick="window.location = '<%=thoatUrl.toString()%>';" />
					</div>
				</td>

			</tr>
		</table>
	</form>


	<%
		if (searchTerms.getTinhId() > 0){
	%>
	<liferay-ui:search-container emptyResultsMessage="no-web-content-were-found" searchContainer="<%=DSTienAnSearch%>">
		<liferay-ui:search-container-results>
			<%
				results = 
				DoiTuongTienAnLocalServiceUtil.findByDS_XoaAnTich
						(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(),
								searchTerms.getTuNgayXoaAnTich(),
								searchTerms.getDenNgayXoaAnTich(), searchContainer.getStart(),
								searchContainer.getEnd(), searchContainer.getOrderByComparator());

				total = totalRow;
				
					
				pageContext.setAttribute("results", results);
				pageContext.setAttribute("total", total);
			%>
		</liferay-ui:search-container-results>

		<%
			int start = DSTienAnSearch.getDelta() * (DSTienAnSearch.getCur() - 1);
		%>

		<liferay-ui:search-container-row className="qldc.pm.tienan.dto.DS_XoaAnTichDTO" keyProperty="soDinhDanhCaNhan" modelVar="xoaAnTich">
			<%
				String soDinhDanhCaNhan = xoaAnTich.getSoDinhDanhCaNhan();
				DoiTuongTienAn doiTuongTienAn = DoiTuongTienAnLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);
				StringBuffer ngayXoaAnTich = new StringBuffer();		 
				List<Date> ngayXoaAnTichs = LichSuXoaAnTichLocalServiceUtil.findByNgayKyDuyetXoaAnTich(null,
						null,doiTuongTienAn.getDoiTuongTienAnId());
				for(int i=0;i<ngayXoaAnTichs.size();i++){
					ngayXoaAnTich.append(sdf.format(ngayXoaAnTichs.get(i)));
					if (i < ngayXoaAnTichs.size()  -1){
						ngayXoaAnTich.append(", ");
					}
				}
				
				
			%>

			<liferay-ui:search-container-column-text cssClass="aui-w3 txt-center" name="stt" value="<%=String.valueOf(++start)%>" />
			<liferay-ui:search-container-column-text cssClass="aui-w15" name="ho-va-ten" property="hoTen" />
			<liferay-ui:search-container-column-text cssClass="txt-center" name="so-dinh-danh-ca-nhan" property="soDinhDanhCaNhan" />
			<liferay-ui:search-container-column-text cssClass="txt-center" name="so-cmnd" property="soCMND" />
			<liferay-ui:search-container-column-text cssClass="txt-center" name="ngay-sinh">
				<%=Validator.isNotNull(xoaAnTich.getNgaySinh()) ? dateFormat.format(xoaAnTich.getNgaySinh()) :""%>
			</liferay-ui:search-container-column-text>
			<liferay-ui:search-container-column-text name="gioi-tinh">
				<liferay-ui:message key="<%=EnumGioiTinh.getKey(xoaAnTich.getGioiTinh())%>" />
			</liferay-ui:search-container-column-text>
			<liferay-ui:search-container-column-text name="dia-chi-thuong-tru" property="diaChi" />
			<liferay-ui:search-container-column-text cssClass="txt-center aui-w10" name="so-lan-xoa-an-tich" property="soLanPhamToi" />
			<liferay-ui:search-container-column-text cssClass="txt-center aui-w10" name="ngay-xoa-an-tich"><%=ngayXoaAnTich %> 	</liferay-ui:search-container-column-text>

		</liferay-ui:search-container-row>

		<liferay-ui:search-iterator searchContainer="<%=DSTienAnSearch%>" />
	</liferay-ui:search-container>
	<%
		} else {
	%>
	<h5>
		<liferay-ui:message key="vui-long-chon-tieu-chi-lap-bao-cao" />
	</h5>
	<%
		}
	%>
</div>
<script type="text/javascript">
$(function() {
	 loadQuanHuyen('<portlet:namespace />noiThuongTruTinhId', '<portlet:namespace />noiThuongTruQuanHuyenId', '<portlet:namespace />noiThuongTruPhuongXaId', '<%=loadQuanHuyen.toString()%>','<%=noiThuongTruQuanHuyenIdSelected%>');
	 loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString()%>', '<%=noiThuongTruPhuongXaIdSelected%>');
});

function timDanhSach() {
		$('#<portlet:namespace/>frm').removeAttr("target");
		$('#<portlet:namespace/>frm').removeAttr("action");
		$('#<portlet:namespace/>cmd').val('<%=TienAnConstants.TIM%>');
		$("#<portlet:namespace/>frm").attr('onsubmit', 'return true;');
		$("#<portlet:namespace/>frm").submit(); 
		
};

function taoBaoCao() {
	$('#<portlet:namespace/>cmd').val('<%=TienAnConstants.IN%>');
		$("#<portlet:namespace/>frm").attr('target', '_blank');
		$("#<portlet:namespace/>frm").submit();
	};
</script>

